<?php
/**
 * 分页类
 */ 
class Page
{
    //数据总页数
    private $page_total_num;
    
    //每一页显示的数量
    private $every_page_num;
    
    //总页数
    private $pages_total;
    
    //limit的具体内容
    public $limit;

    public $limit2;
    
    //url参数名称
    public $page_url_name;
    
    // 分页语言包
    public $language=array(
            'first_page'    =>'首页',
            'pre_page'      =>'上一页',
            'next_page'     =>'下一页',
            'last_page'     =>'尾页'
    );
    //原url的名称
    private $url_old;
    
    /* 构造函数 */
    public function __construct($arr)
    {
        //数据总数
        $this->page_total_num       =$arr['page_total_num'];
        //每页要显示的数量
        $this->every_page_num       =$arr['every_page_num'];
        //url参数
        $this->page_url_name        =$arr['page_url_name'];
        
        //1 计算总页数
        $this->pages_total=ceil($this->page_total_num/$this->every_page_num);
        //2 设置当前页面
        empty($_GET[$this->page_url_name])? $_GET[$this->page_url_name]=1:$_GET[$this->page_url_name]=intval($_GET[$this->page_url_name]);
        
        //3检查当前页面的合理性
        if($_GET[$this->page_url_name] < 1){$_GET[$this->page_url_name]=1;}
        if($_GET[$this->page_url_name] > $this->pages_total){$_GET[$this->page_url_name] = $this->pages_total;}
        
        //4 计算limit前后的内容
        $this->limit=' limit '.(($_GET[$this->page_url_name]-1)*$this->every_page_num).','.$this->every_page_num;
        $this->limit2=$this->every_page_num.','.(($_GET[$this->page_url_name]-1)*$this->every_page_num);
        //5 实现原有url参数的保持
        // 5.1 http://localhost/xxx.php //本身不带有url参数 现在的程序能够满足
        // 5.2 http://localhost/xxx.php?xx=xx&yy=yy
        //   解决办法 保持原有url http://localhost/index.php
        if(count($_GET) >1)
        {
            $this->url_old='?';
            foreach($_GET as $k=>$v)
            {
                if($k != $this->page_url_name)
                {
                   $this->url_old .=$k.'='.$v.'&';
                }
            }
        }
        else
        {
            $this->url_old='?';
        }
    }
    
    //首页
    public function first_page()
    {
        return '<a href="'.$this->url_old.$this->page_url_name.'=1">'.$this->language['first_page'].'</a>';
    }
    //上一页
    public function pre_page()
    {
        return '<a href="'.$this->url_old.$this->page_url_name.'='.($_GET[$this->page_url_name]-1).'">'.$this->language['pre_page'].'</a>';
    }
    
    //下一页
    public function next_page()
    {
        return '<a href="'.$this->url_old.$this->page_url_name.'='.($_GET[$this->page_url_name]+1).'">'.$this->language['next_page'].'</a>';
    }
    //尾页
    public function last_page()
    {
        return '<a href="'.$this->url_old.$this->page_url_name.'='.$this->pages_total.'">'.$this->language['last_page'].'</a>';
    }
    
    //中间过渡页
    public function more_page()
    {
        $str='';
        if($_GET[$this->page_url_name] <= 4)
        {
            $start  =1;
            $end    =10;   
        }
        else
        {
            $start  =$_GET[$this->page_url_name]-3;
            $end    =$_GET[$this->page_url_name]+6;               
        }
        
        if($end > $this->pages_total)
        {
            $end =$this->pages_total;
        }
        
        for($i=$start;$i<=$end;$i++)
        {
             //如果循环页 == 当前页 加上一个特殊的class
             if($i == $_GET[$this->page_url_name])
             {
                 $str.='<a href="'.$this->url_old.$this->page_url_name.'='.$i.'" class="page_c">'.$i.'</a>';   
             }
             else
             {
                $str.='<a href="'.$this->url_old.$this->page_url_name.'='.$i.'">'.$i.'</a>';   
             }
             
        }
        return $str;
    }
    
    //跳转页
    public function skip_page()
    {
        $str ='<select onchange="location.href=\''.$this->url_old.$this->page_url_name.'=\'+this.value">';
        for($i=1;$i<=$this->pages_total;$i++)
        {
            if($_GET[$this->page_url_name] == $i)
            {
                $str .='<option value="'.$i.'" selected="selected" > 第'.$i.'页 </option>';
            }else
            {
                $str .='<option value="'.$i.'"> 第'.$i.' 页</option>';
            }
        }
        $str .='</select>';
        return $str;
    }
    
    //综合方法
    public function pages_all()
    {
        return $this->first_page().$this->pre_page().$this->more_page().$this->next_page().$this->last_page();
    }
    //获取信息
    public function page_info()
    {
        $pageinfo=  array(
            //总的条目数
            $this->page_total_num,
            //总页数
            $this->pages_total,
            //当前页是
            $_GET[$this->page_url_name],
            //每页显示条目数
            $this->every_page_num
        );
        
        return $pageinfo;
    }   
}
